﻿//

using System;
using System.Collections.Generic;
using System.Web;
using DotNetWikiBot;
using System.Threading;
using System.Collections;
using System.IO;
using System.Text;


namespace wikibot
{

    class Program
    {
        bool OK = true;
        
        public static class Config
        {
            public static bool OK = true;
            public static String username = "";
            public static String password = "";
            public static bool SaveLog = false;
            public static String LogPath = "log1.txt";
            public static String IrcRC = "";
            public static bool IRCEnabled;
        };

    public class t
    {
          public	DotNetWikiBot.Site testwp; // test.wikipedia.org
          public	DotNetWikiBot.Site dewiki;
          public	DotNetWikiBot.Site meta;
          public	DotNetWikiBot.Site co;
          public	DotNetWikiBot.Site enwiki;
          public  bool active;
          public t()
          {
              active = false;
	        }
          public int Log (String message)
          {
             message=message.Insert(0, "SYSTEM: "); 
             message = DateTime.Now + "  " + message;
             Console.WriteLine(message);
             return 0;
          }

          public bool TLog (int id, String message)
          {
	            message=message.Insert(0, "TASK: ");
	            Console.WriteLine(message);
	            return false; 
         }
         public int Do()
         {
           TLog(1, "error: nothing to do");
           return 1;
         }
         public bool LogP ( String message)
         { // private logs
           Console.WriteLine(message);
           return true; 
         }
         public void Init()
         {
         }
    };



public class task_1 : t
{ //every task should be inherited from base

};

public  class task_2 : t
{
  public int Check()
	{
     DotNetWikiBot.Page shut = new DotNetWikiBot.Page(enwiki, "Special:MyPage/Shutdown");
     shut.Load();
     if (shut.text.Contains("if you want to shutdown this bot change this to the shutdown reason and bot will be terminated") == false)
     {
       Log("Terminated with: " + shut.text);
       active = false;
       String path = Config.LogPath;
       String s = "";
       System.IO.StreamReader sw = System.IO.File.OpenText(path);
       DotNetWikiBot.Page bp = new DotNetWikiBot.Page(testwp, "User:Petan-Bot/Status");
       bp.Load();
       bp.text = "System log:\n\n<pre>";
       while ( sw.EndOfStream != true )
       {
          s = sw.ReadLine();
          bp.text = bp.text + "\n" + s;
       }
       bp.text = bp.text + "</pre>";
       bp.Save();
       Config.OK = false;
     }
	return 2;
	}

	public int Do()
	{
     Check();
     System.IO.StreamReader sw = System.IO.File.OpenText(Config.LogPath);
     String s = "";
     DotNetWikiBot.Page bp = new DotNetWikiBot.Page(testwp, "Special:MyPage/Status");
     bp.Load();
     bp.text = "System log:\n\n<pre>";
     try
     {
       while ( sw.EndOfStream != true )
       {
         s = sw.ReadLine();
         bp.text = bp.text + "\n" + s;
       }
       bp.text = bp.text + "</pre>";
       bp.Save();
     }
         catch (Exception x ) 
         {
           TLog(2, "can't save" );
         }
     if (active == false)
     {
         Config.OK = false;
     }
     Thread.Sleep(12000);
     return 0;
	}
	public void Init()
	{
		TLog(2, "Loading task #2");
		active = true;
	}

};



public  class task_4 : t
{ //example task
	public void Init()
	{
		TLog(6, "Initialising task #18");
		active = true;
		TLog(6, "OK");
	}
	public int Do()
	{
		DotNetWikiBot.Page sb = new Page(enwiki, "Project:Huggle/Sandbox");
		sb.Load();
		if ( !sb.text.Contains("''Feel free to test [[Wikipedia:Huggle|Huggle]] here at any time for any reason.''") )
		{
			sb.text = "''Feel free to test [[Wikipedia:Huggle|Huggle]] here at any time for any reason.''\n<br>'''NOTE: Admins - If this page gets tagged for deletion DO NOT delete it as it is more than likely a test tag'''<br>\n'''NOTE: Users - If you tag this page for deletion as a test please remember to remove the tag after your test'''\n<br><!-- please do not remove -->\n\n\n\n" + sb.text;
			sb.Save(sb.text, "Restoring sandbox ([[WP:Bots|bot]])", false);
		}
		return 2;
	}
};


public  class PBot : Bot
{
   public	String login = "";
   public	String pw = "";
   public	bool started;
   public	DotNetWikiBot.Site testwp;
   public	DotNetWikiBot.Site dewiki;
   public	DotNetWikiBot.Site meta;
   public	DotNetWikiBot.Site commons;
   public	DotNetWikiBot.Site enwiki;
   public	DotNetWikiBot.Site cswiki;
   public	System.Threading.Thread t1;
   public	System.Threading.Thread t2;
   public	System.Threading.Thread t4;
   public	task_1 job_x1;
   public	task_2 job_x2;
   public	task_4 job_x4;

   public int Log(String message)
   {
     message = message.Insert(0, "SYSTEM: ");
     message = DateTime.Now + "  " + message;
     Console.WriteLine(message);
     return 0;
   }

   public bool TLog(int id, String message)
   {
      message = message.Insert(0, "TASK: ");
      Console.WriteLine(message);
      return false;
   }

	public void d1 ()
	{
     while (Config.OK == true )
     {
        job_x1.Do();
        Thread.Sleep(500000);
     }
	}
	public void d2()
	{
        while (Config.OK == true )
        {
          job_x2.Do();
        }
	}
	public void d4 ()
	{
        while (Config.OK == true)
        {
          job_x4.Do();
        }
   }
		public void Initialise()
        {
            Log("Started wikibot core version 1.0, timeout disabled");
            Log("Connecting to english wp...");
            enwiki = new Site("en.wikipedia.org", login, pw);
            Log("Connecting to german wiki...");
            dewiki = new Site("de.wikipedia.org", login, pw);
            Log("Connecting to test wp...");
            testwp = new Site("test.wikipedia.org", login, pw);
            Log("Tasks...");
            job_x1 = new task_1();
            job_x1.enwiki = enwiki;
            job_x1.testwp = testwp;
            job_x2 = new task_2();
            job_x2.testwp = testwp;
            job_x2.enwiki = enwiki;
            job_x4 = new task_4();
            job_x4.enwiki = enwiki;
            job_x4.dewiki = dewiki;
            job_x4.testwp = testwp;
			job_x1.Init();
            job_x2.Init();
            job_x4.Init();
            started = true; // initialisation is done
	    }
};


public class Kernel_Main
{
public Kernel_Main()
{
    Start();
}
public static void Start()
    {
        PBot Pb = new PBot();
        
        // Create the thread object, passing in the
        // serverObject.InstanceMethod method using a
        // ThreadStart delegate.
		try
		{
			Pb.Initialise();

			Pb.t1 = new Thread(new ThreadStart(Pb.d1));
			Pb.t2 = new Thread(new ThreadStart(Pb.d2));
			Pb.t4 = new Thread(new ThreadStart(Pb.d4));
		  // Start all tasks
      Pb.t1.Start();
      Pb.t2.Start();
		
		} catch (Exception em)
		{
        Console.WriteLine(" error ");
		}


        while (Config.OK == true)
        {
             Thread.Sleep(3400);
        }
          Pb.t1.Abort();
          Pb.t2.Abort();
          Pb.t4.Abort();

        // Start the thread.
    }
};


        static void Main(string[] args)
        {
            Kernel_Main bot = new Kernel_Main();
        }
    }
}
